package com.soft863.tc.cahce;

import com.soft863.tc.common.constant.ValueConstant;
import com.soft863.tc.common.util.ConnectionUtil;
import redis.clients.jedis.Jedis;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/*
 * 向Redis增加缓存数据
 * */
public class Bootstrap {
    private static final Map<String, Integer> userMap = new HashMap<>();
    private static final Map<String, Integer> dateMap = new HashMap<>();

    public static void main(String[] args) {
        getDate();
        saveCache();

    }

    //封装mysql数据
    private static void getDate() {
        String queryUserSql = "select id,tel from tc_user";
        String queryDateSql = "select id,year,month,day from tc_date";
        Connection con = ConnectionUtil.getMySQLConnection();
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            statement = con.createStatement();
            resultSet = statement.executeQuery(queryUserSql);
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String tel = resultSet.getString(2);
                userMap.put(tel, id);
            }
            resultSet = statement.executeQuery(queryDateSql);
            int id = 0;
            String year = "";
            String month = "";
            String day = "";
            while (resultSet.next()) {
                id = resultSet.getInt(1);
                year = resultSet.getString(2);
                month = resultSet.getString(3);
                day = resultSet.getString(4);
                dateMap.put(concatDate(year, month, day), id);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                ConnectionUtil.closeMySQL();
            }
        }
    }

    private static void saveCache() {
        Jedis jedis = ConnectionUtil.getJedis();
        for (Map.Entry<String, Integer> next : userMap.entrySet()) {
            String key = next.getKey();
            Integer value = next.getValue();
            jedis.hset(ValueConstant.REDIS_HASH_USER, key, "" + value);

        }
        for (Map.Entry<String, Integer> next : dateMap.entrySet()) {
            String key = next.getKey();
            Integer value = next.getValue();
            jedis.hset(ValueConstant.REDIS_HASH_DATE, key, "" + value);
        }
        ConnectionUtil.closeJedis();
    }

    private static String concatDate(String year, String month, String day) {
        if (month.length() == 1) {
            month = "0" + month;
        }
        if (day.length() == 1) {
            day = "0" + day;
        }
        return year + month + day;
    }
}
